{% schema %}
{
  "name": "Tailored Vision",
  "settings": [
    {
      "type": "text",
      "id": "tailored_title",
      "label": "Title",
      "default": "Tailored Vision"
    },
    {
      "type": "text",
      "id": "tailored_desc",
      "label": "Description",
      "default": "Seamless built-in correction from 0.00D to -6.00D"
    },
    {
      "type": "url",
      "id": "img_device_src",
      "label": "Device Image Source",
    },
    {
      "type": "url",
      "id": "img_device_pc_src",
      "label": "Device Image Source",
    },
    {
      "type": "url",
      "id": "img_leno_src",
      "label": "Leno Image Source",
    },
    {
      "type": "text",
      "id": "tailored_text",
      "label": "Additional Text",
      "default": "Easily swap insert lenses with precision prescription—perfect for sharing with friends and family"
    }
  ]
}
{% endschema %}

<div class="tail-{{ section.id }}">
  <div class="container">
    <div class="title-{{ section.id }} anim">{{ section.settings.tailored_title }}</div>
    <div class="desc-{{ section.id }} anim">{{ section.settings.tailored_desc }}</div>
    <div class="tail-600-{{ section.id }} anim-filter">
      0-<span id="tail-600">600</span>°
    </div>
    <div class="devices-{{ section.id }}">
      <img class="d-lg-none devices-img-{{ section.id }}" src="{{ section.settings.img_device_src }}" />
      <img class="d-lg-block d-none devices-img-{{ section.id }}" src="{{ section.settings.img_device_pc_src }}" />
    </div>
    <div class="len-{{ section.id }}">
      <img class="len-img-{{ section.id }}" src="{{ section.settings.img_leno_src }}" />
      <div class="len-text-{{ section.id }}">{{ section.settings.tailored_text }}</div>
    </div>
  </div>
</div>

<style>
  .tail-{{ section.id }} {
    width: 100%;
    padding-bottom: 110px;
    background: black;
    overflow: hidden;
    position: relative;
  }
  .title-{{ section.id }} {
    font-family: HarmonyOS Sans;
    font-size: 36px;
    font-weight: bold;
    line-height: 100%;
    text-align: left;
    letter-spacing: -1.44px;
    color: #FFFFFF;
    margin-bottom: 25px;
  }
  .desc-{{ section.id }} {
    width: 262px;
    font-family: HarmonyOS Sans;
    font-size: 20px;
    font-weight: normal;
    line-height: 120%;
    letter-spacing: -0.8px;
    font-variation-settings: "opsz" auto;
    color: #FFFFFF;
    margin-bottom: 10px;
  }
  .tail-600-{{ section.id }} {
    position: relative;
    top: 45px;
    width: 100%;
    height: 100px;
    font-family: HarmonyOS Sans;
    font-size: 96px;
    font-weight: bold;
    line-height: 108%;
    text-align: center;
    letter-spacing: -1.44px;
    font-variation-settings: "opsz" auto;
    background: linear-gradient(180deg, #FAFAFA 0%, #627599 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-fill-color: transparent;
  }
  .devices-{{ section.id }} {
    position: relative;
    width: 100%;
    height: 380px;
    margin-left: -24px;
  }
  .devices-img-{{ section.id }} {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    transform: rotate(-7.40966deg);
  }
  .len-{{ section.id }} {
    display: flex;
    align-items: center;
  }
  .len-img-{{ section.id }} {
    width: 78px;
    height: 47px;
    margin-right: 20px;
  }
  .len-text-{{ section.id }} {
    font-family: HarmonyOS Sans SC;
    font-size: 14px;
    font-weight: normal;
    line-height: 120%;
    letter-spacing: 0px;
    font-variation-settings: "opsz" auto;
    color: #86868B;
  }

  @media screen and (min-width: 1152px) {
    .title-{{ section.id }} {
      font-family: HarmonyOS Sans;
      font-size: 70px;
      font-weight: bold;
      line-height: 100%;
      letter-spacing: 0em;
      color: #FFFFFF;
      margin-bottom: 30px;
      margin-top: 200px;
    }
    .desc-{{ section.id }} {
      width: 427px;
      font-size: 32px;
      font-weight: normal;
      line-height: normal;
      letter-spacing: 0px;
      color: #FFFFFF;
      margin-bottom: 197px;
    }
    .tail-600-{{ section.id }} {
      width: 791px;
      height: 220px;
      top: 0;
      margin-bottom: 74px;
      text-align: left;
      font-size: 200px;
    }
    .devices-{{ section.id }} {
      position: absolute;
      width: 943px;
      height: 1207px;
      margin-left: 0;
      right: 0;
      bottom: 100px;
      max-width: 50%;
    }
    .len-{{ section.id }} {
      margin-bottom: 489px;
      width: 420px;
    }
  }

</style>

<script
  type="module"
>
  import { CountUp } from "{{ 'countUp.min.js' | asset_url }}";
  
  document.addEventListener("DOMContentLoaded", function () {
    var countUp = new CountUp("tail-600", 600, {
      onCompleteCallback: () => {
            flag  = 2;
          },
          onStartCallback: () => {
            flag  = 1;
          }
    });
    const targetDiv = document.getElementById('tail-600');
    let flag = 0;
    function addClassWhenInCenter() {
        const viewportHeight = window.innerHeight;
        const targetRect = targetDiv.getBoundingClientRect();
        const targetTop = targetRect.top;
        const targetBottom = targetRect.bottom;
    
        if (targetTop < viewportHeight && targetBottom > 0) {
          flag == 0 && countUp.start();
        } else if (targetTop > viewportHeight || targetBottom < 0) {
          flag == 2 && countUp.reset();
            flag = 0;
        }
    }
  
    window.addEventListener('scroll', addClassWhenInCenter);
    addClassWhenInCenter();
    
    const anim = [
      ".anim", ".anim-filter", ".anim-rotate15"
    ]
    const contents = [];
    anim.forEach(item => {
      const arr = document.querySelectorAll(item);
      arr.forEach(a => contents.push(a))
    })
    const anim_remove = [".anim-remove"];
    console.log("anim_remove", anim_remove)
    const removes = [];
    anim_remove.forEach(item => {
      const arr = document.querySelectorAll(item);
      arr.forEach(a => removes.push(a))
    })

    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          entry.target.classList.add('animated');
 
        } else {
          entry.target.classList.remove('animated');
 
        }
      });
    }, {
    threshold: 0
    });

    const observer_remove = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          entry.target.classList.remove('anim-remove');
        } else {
          entry.target.classList.add('anim-remove');
        }
      });
    }, {
    threshold: 0
    });

    contents.forEach(content => observer.observe(content)); // 观察每一个 元素
    removes.forEach(content => observer_remove.observe(content));

    // 滑动旋转
    const targets = document.querySelectorAll(".devices-img-{{ section.id }}");
    window.addEventListener("scroll", () => {
      targets.forEach(target => {
        const targetRect = target.getBoundingClientRect();
        const viewportHeight = window.innerHeight;
        const targetCenterY = targetRect.top + targetRect.height / 2;
        const percentageInViewport = targetCenterY / viewportHeight;
        if (percentageInViewport >= 0.2 && percentageInViewport <= 0.9) {
          const rotationAngle = ((percentageInViewport - 0.3) / 0.4) * 5;
          target.style.transform = `rotate(-${rotationAngle}deg)`;
        }
      })
    });
  });
</script>














